package cn.newgrand.netcall.offlinefiletransfer;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Packet;

import cn.newgrand.netcall.offlinefiletransfer.OfflineFileTransferRequest;
import cn.newgrand.netcall.offlinefiletransfer.OfflineTransferManager;
import cn.newgrand.smackx.packet.OfflineProxyPacket;

public class OfflineFileProxyListener implements PacketListener {

	@Override
	public void processPacket(Packet packet) {
		// TODO Auto-generated method stub
		OfflineProxyPacket info = (OfflineProxyPacket)packet;
    	String proxyIP ="";
    	String proxyPort = "";
    	String proxyJID = "";
    	String from = info.getFrom();
    	String to = info.getTo();
    	OfflineProxyPacket.HostInfo hostinfo =info.getHostInfo();
    	if(hostinfo!=null){
			proxyJID = hostinfo.getProxyJID();
			proxyIP = hostinfo.getProxyIP();
			proxyPort = hostinfo.getProxyPort();
			String fileName, fileSize, fileDesc, initiator, md5,dataString;
			for(OfflineProxyPacket.FileInfo fi:info.getList()){
    			fileName = fi.getFileName();
    			fileSize = fi.getFileSize();
    			fileDesc = fi.getFileDesc();
    			initiator = fi.getInitiator();
    			md5 = fi.getMd5();
    			dataString = fi.getDataString();
    			OfflineFileTransferRequest of = new OfflineFileTransferRequest(fileName, fileSize, fileDesc, initiator, md5, from,proxyIP,proxyPort,dataString);
    			of.setSession("jsi_"+Math.abs(OfflineFileTransferRequest.randomGenerator.nextLong()));
    			OfflineTransferManager.handleTransferRequest(of);
			}
    	}
	}

}
